const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceCollege = require("../../../service/service_college.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    categoryId: null,
    navDate:[],
    swiperDatas:[],
    news:[],
    /**
     * 开始页
     */
    startPage: 0
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this;
      _this._showData(null);
    },

    _showData: function (_categoryId){
      const _this = this;
      /**
       * 分类
       */
      ServiceCollege.categoryList(function (data) {
        _this.setData({
          navDate: data.records
        })
        // 
        if (data.records.length > 0) {
          const categoryId = null !== _categoryId ? _categoryId : data.records[0].id;
          _this.setData({ categoryId });
          /** 轮播图 */
          ServiceCollege.categoryFindById(categoryId, function (data) {
            const swiperDatas = [];
            for (let img of data.images) {
              swiperDatas.push({
                swipeSrc: _this.data.b_service + img.imgUrl,
                swiperTitle: img.title,
                swiperUrl: img.url
              });
            }
            _this.setData({ swiperDatas });
          });
          /** 新闻 */
          ServiceCollege.page(categoryId, 0, function (data) {
            _this.setData({ 
              news: data.records,
              startPage: 0
            });
          });
        }
      });
    },

    onSelectCategory:function(e){
      const _this = this;
      const categoryId = e.currentTarget.dataset.id;
      _this._showData(
        categoryId
      );
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
      const _this = this;
      let startPage = _this.data.startPage + 1;
      const categoryId = _this.data.categoryId;
      ServiceCollege.page(categoryId, startPage, function (data) {
        _this.data.news.push(...data.records);
        _this.setData({
          startPage: startPage,
          news: _this.data.news
        });
      });
    }
  }

})